
<template>
  <div class="about">
    <h1>用户管理</h1>
    <el-button type="success" @click="addShow">添加用户</el-button>
    <!-- 表格 -->
    <el-table :data="userData" style="width: 100%">
      <el-table-column prop="id" label="编号" width="180"> </el-table-column>
      <el-table-column prop="username" label="用户" width="180">
      </el-table-column>
      <el-table-column prop="sex" label="性别"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <el-pagination
      :page-size="pageParam.pageSize"
      layout="prev, pager, next"
      :total="total"
      @next-click="next"
    >
    </el-pagination>
    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <span>
        <el-form ref="form" :rules="rules" :model="user" label-width="80px">
          <el-form-item label="用户" prop="username">
            <el-input v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" v-if="showpwd">
            <el-input type="password" v-model="user.password"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入对象
import { getUsers, addUser, deleteUser, updateUser } from "@/api/user.js";

export default {

  data() {
    return {
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          // { min: 6, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
        ],
      },
      dialogVisible: false,
      title: "添加用户",
      showpwd: true,
      total: 0,
      pageParam: { currentPage: 1, pageSize: 3 },
      user: { username: "", password: "" },
      userData: [
        { id: 1, username: "admin", sex: "男" },
        { id: 2, username: "system", sex: "女" },
      ],
    };
  },
  created() {
    // 1. 创建dom对象完成，加载后端数据
    this.loadData(this.pageParam);
  },
  methods: {
    loadData(pageParam) {
      // 2. 加载后端数据
      getUsers(pageParam).then((response) => {
        console.log(response);
        if (response.data.code == "0000") {
          this.userData = response.data.data.records;
          this.total = Number(response.data.data.total);
        }
      });
    },
    next(val) {
      console.log(val);
      this.pageParam.currentPage = val;
      this.loadData(this.pageParam);
    },
    save() {
      this.$refs["form"].validate((valid) => {
        //
        if (valid) {
          if (this.title == "添加用户") {
            addUser(this.user).then((response) => {
              if (response.data.code == "0000") {
                this.$message.success(response.data.msg);
                this.dialogVisible = false;
              } else {
                this.$message.error(response.data.msg);
              }
            });
          } else {
            updateUser(this.user).then((response) => {
              if (response.data.data.msg == "ok") {
                this.$message.success(response.data);
                this.dialogVisible = false;
              } else {
                this.$message.error(response.data);
              }
            });
          }
          //
        } else {
          return false;
        }
      });
    },
    handleEdit(index, row) {
      this.dialogVisible = true;
      this.showpwd = false;
      this.user = row;
      this.title = "修改用户";
    },
    handleDelete(index, row) {
      this.$confirm("你确认要删除" + row.username + ", 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteUser(row.id).then((response) => {
            console.log(response.data);
            // 删除成功
            this.loadData();
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addShow() {
      this.title = "添加用户";
      this.showpwd = true;
      this.dialogVisible = true;
      this.user = {};
    },
  },
};
</script>
